package org.demo.service.impl;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Lock;

import javax.imageio.ImageIO;

import org.apache.commons.lang.math.RandomUtils;

public class Person implements Runnable{
	
	private Lock lock;
	private CountDownLatch latch;
	
	public Person() {}
	
	public Person(Lock lock,CountDownLatch latch) {
		this.lock = lock;
		this.latch = latch;
	}
	
	public void run() {
		try {
			latch.await();
			lock.lock();
			System.out.println(Thread.currentThread().getName()+"======>加锁");
			int get = RandomUtils.nextInt(10) + 1;
			SimpleTest.num -= get;
			System.out.println(Thread.currentThread().getName()+"======>"+get);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}finally {
			System.out.println(Thread.currentThread().getName()+"======>解锁");
			lock.unlock();
		}
		
	}

	
	public static void captureScreen(String fileName) throws Exception {
		 
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Rectangle screenRectangle = new Rectangle(screenSize);
        Robot robot = new Robot();
        BufferedImage image = robot.createScreenCapture(screenRectangle);
        ImageIO.write(image, "png", new File(fileName));
 
    }
 
    public static void main(String[] args) {
        try {
            captureScreen("C:\\Users\\spark\\Desktop\\23333.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
